package main

import "fmt"

func adder() func(int) int {
	// 自由变量
	sum := 0
	return func(i int) int {
		// 局部变量
		sum += i
		return sum
	}
}

type iAdder func(int) (int, iAdder)

func adder2(base int) iAdder {
	return func(v int) (int, iAdder) {
		return base + v, adder2(base + v)
	}
}

func main() {
	a := adder()
	for i := 0; i < 10; i++ {
		fmt.Println(a(i))
	}

	//a := adder2(0)
	//for j := 0; j < 10; j++ {
	//	var s int
	//	s, a := a(j)
	//	fmt.Printf("0 + 1 + ... + %d = %d\n", j, s)
	//}
}
